package com.yidu.labyrinth.service;

import com.yidu.labyrinth.dao.LabyrinthUserDao;
import com.yidu.labyrinth.main.LabyrinthRoleEntity;
import com.yidu.labyrinth.main.LabyrinthRoleOrUserEntity;
import com.yidu.labyrinth.main.LabyrinthUserEntity;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;

/**
 * @描述：
 * @ClassName:labyrinth
 * @backage:com.yidu.labyrinth.service
 * @Date:2019 2019/9/25 13:47
 * @Author:易嵘
 **/
public class LabyrinthUserServiceImpl implements LabyrinthUserService {

    Logger logger=Logger.getLogger(LabyrinthUserServiceImpl.class);

    private LabyrinthUserDao labyrinthUserDao;

    public LabyrinthUserDao getLabyrinthUserDao() {
        return labyrinthUserDao;
    }

    public void setLabyrinthUserDao(LabyrinthUserDao labyrinthUserDao) {
        this.labyrinthUserDao = labyrinthUserDao;
    }

    /**
     * 增加
     *
     * @param labyrinthVo
     * @return
     */
    @Override
    public int add(LabyrinthVo labyrinthVo) {
        labyrinthVo.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
        labyrinthVo.setUserCreateDate(new java.sql.Date(System.currentTimeMillis()));
        labyrinthVo.setUserCreateUser("admin");
        labyrinthVo.setUserResult(1);
        LabyrinthUserEntity user = addOrUpdate(labyrinthVo);
        int rows = labyrinthUserDao.add(user);
        return updateOradd(rows,user,labyrinthVo);
    }

    /**
     * 增加用户角色关联
     * @param rowss
     * @param labyrinthVo
     * @return
     */
    private int updateOradd(int rowss,LabyrinthUserEntity userPo,LabyrinthVo labyrinthVo){
        if(rowss!=1){
            return rowss;
        }
        String[] ids = labyrinthVo.getRoleId().split(",");
        for (String id : ids) {
            LabyrinthRoleOrUserEntity labyrinthRoleOrUserEntity = new LabyrinthRoleOrUserEntity();
            labyrinthRoleOrUserEntity.setRoleOrUserId(UUID.randomUUID().toString().replaceAll("-", ""));
            labyrinthRoleOrUserEntity.setLabyrinthUserEntity(userPo);
            LabyrinthRoleEntity labyrinthRoleEntity = new LabyrinthRoleEntity();
            labyrinthRoleEntity.setRoleId(id);
            labyrinthRoleOrUserEntity.setLabyrinthRoleEntity(labyrinthRoleEntity);
            int row = labyrinthUserDao.addUserOrRole(labyrinthRoleOrUserEntity);
            if (row != 1) {
                return row;
            }
        }
        return 1;
    }

    /**
     * 修改
     * @param labyrinthVo
     * @return
     */
    @Override
    public int update(LabyrinthVo labyrinthVo) {

        logger.info(labyrinthVo.getUserName()+"  userName用户名称====================One");
        LabyrinthUserEntity user = labyrinthUserDao.findById(labyrinthVo.getUserId()) ;
        user.setUserId(labyrinthVo.getUserId());
        user.setUserName(labyrinthVo.getUserName());
        user.setUserPhone(labyrinthVo.getUserPhone());
        user.setUserMail(labyrinthVo.getUserMail());
        user.setUserPwd(labyrinthVo.getUserPwd());
        user.setUserSort(labyrinthVo.getUserSort());
        int row= labyrinthUserDao.update(user);

        if(row!=1){
            return row;
        }
        int rows=labyrinthUserDao.deleteUserOrRole(labyrinthVo.getUserId());
        return updateOradd(rows,user,labyrinthVo);
    }

    /**
     * vo转换po
     *
     * @param labyrinthVo
     * @return
     */
    private LabyrinthUserEntity addOrUpdate(LabyrinthVo labyrinthVo) {
        LabyrinthUserEntity labyrinthUserEntity = new LabyrinthUserEntity();
        BeanUtils.copyProperties(labyrinthVo, labyrinthUserEntity);
        return labyrinthUserEntity;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<LabyrinthVo> findAll(LabyrinthVo labyrinthVo, Pages pages) {
        LabyrinthUserEntity labyrinthUserEntity = new LabyrinthUserEntity();
        BeanUtils.copyProperties(labyrinthVo, labyrinthUserEntity);
        List<LabyrinthVo> labyrinthVoList = new ArrayList<>();
        List<LabyrinthUserEntity> labyrinthUserEntityList = labyrinthUserDao.findAll(labyrinthUserEntity, pages);
        for (LabyrinthUserEntity labyrinthUserEntity1 : labyrinthUserEntityList) {
            LabyrinthVo labyrinthVo1 = new LabyrinthVo();
            BeanUtils.copyProperties(labyrinthUserEntity1, labyrinthVo1);
            labyrinthVoList.add(labyrinthVo1);
        }
        return labyrinthVoList;
    }

    @Override
    public Integer userCount(LabyrinthVo labyrinthVo) {
        return labyrinthUserDao.userCount(addOrUpdate(labyrinthVo)).intValue();
    }

    @Override
    public Map<String, Object> findById(String id) {
        Map<String, Object> map = new HashMap<>();
        LabyrinthUserEntity labyrinthUserEntity = labyrinthUserDao.findById(id);
        LabyrinthVo labyrinthUserVo = new LabyrinthVo();
        BeanUtils.copyProperties(labyrinthUserEntity, labyrinthUserVo);
        map.put("user", labyrinthUserVo);

        List<LabyrinthRoleOrUserEntity> labyrinthRoleOrUserEntities = labyrinthUserDao.findAllUserOrRole(id);
        List<LabyrinthVo> labyrinthVos = new ArrayList<>();
        for (LabyrinthRoleOrUserEntity labyrinthRoleOrUserEntity : labyrinthRoleOrUserEntities) {
            LabyrinthVo labyrinthVo = new LabyrinthVo();
            labyrinthVo.setRoleOrUserId(labyrinthRoleOrUserEntity.getRoleOrUserId());
            labyrinthVo.setUserId(labyrinthRoleOrUserEntity.getLabyrinthUserEntity().getUserId());
            labyrinthVo.setRoleId(labyrinthRoleOrUserEntity.getLabyrinthRoleEntity().getRoleId());
            labyrinthVos.add(labyrinthVo);
        }
        map.put("roleOrUser", labyrinthVos);
        return map;
    }

    @Override
    public List<LabyrinthVo> findAllRole() {
        List<LabyrinthRoleEntity> labyrinthRoleEntityList = labyrinthUserDao.findAllRole();
        List<LabyrinthVo> labyrinthVoList = new ArrayList<>();
        for (LabyrinthRoleEntity labyrinthRoleEntity : labyrinthRoleEntityList) {
            LabyrinthVo labyrinthVo = new LabyrinthVo();
            BeanUtils.copyProperties(labyrinthRoleEntity, labyrinthVo);
            labyrinthVoList.add(labyrinthVo);
        }
        return labyrinthVoList;
    }

    /**
     * 登录
     * @param userName
     * @param userPwd
     * @param request
     * @return
     */
    @Override
    public int login(String userName, String userPwd, HttpServletRequest request) {
        LabyrinthUserEntity labyrinthUserEntity= labyrinthUserDao.login(userName,userPwd);
        if(labyrinthUserEntity==null){
            return 0;
        }else{
            HttpSession session=request.getSession();
            session.setAttribute("user",labyrinthUserEntity);
            return 1;
        }

    }

    @Override
    public int result(LabyrinthVo labyrinthVo) {
        LabyrinthUserEntity labyrinthUserEntity=labyrinthUserDao.findById(labyrinthVo.getUserId());
        labyrinthUserEntity.setUserResult(labyrinthVo.getUserResult());
        return labyrinthUserDao.update(labyrinthUserEntity);
    }

}